// settings.js
Page({
  data: {
    settings: {
      notifications: true,
      autoPlay: false,
      downloadWifiOnly: true,
      fontSize: 'medium',
      language: 'zh-CN',
      cacheSize: '25.6MB'
    },
    userInfo: {
      isLogin: false
    }
  },

  onLoad() {
    this.loadSettings()
    this.loadUserInfo()
  },

  // 加载设置
  loadSettings() {
    // 从本地存储加载设置
    const settings = wx.getStorageSync('appSettings') || {
      notifications: true,
      autoPlay: false,
      downloadWifiOnly: true,
      fontSize: 'medium',
      language: 'zh-CN',
      cacheSize: '0MB'
    }
    this.setData({ settings })
  },

  // 加载用户信息
  loadUserInfo() {
    const app = getApp()
    const userInfo = app.getUserInfo()
    this.setData({ userInfo })
  },

  // 切换通知设置
  onNotificationChange(e) {
    const value = e.detail.value
    this.updateSetting('notifications', value)
  },

  // 切换自动播放
  onAutoPlayChange(e) {
    const value = e.detail.value
    this.updateSetting('autoPlay', value)
  },

  // 切换WiFi下载
  onDownloadWifiChange(e) {
    const value = e.detail.value
    this.updateSetting('downloadWifiOnly', value)
  },

  // 更新设置
  updateSetting(key, value) {
    const settings = { ...this.data.settings, [key]: value }
    this.setData({ settings })
    wx.setStorageSync('appSettings', settings)
    
    wx.showToast({
      title: '设置已保存',
      icon: 'success'
    })
  },

  // 字体大小设置
  onFontSizeTap() {
    const sizes = [
      { key: 'small', name: '小' },
      { key: 'medium', name: '中' },
      { key: 'large', name: '大' }
    ]
    
    const itemList = sizes.map(item => item.name)
    const currentIndex = sizes.findIndex(item => item.key === this.data.settings.fontSize)
    
    wx.showActionSheet({
      itemList,
      success: (res) => {
        const selectedSize = sizes[res.tapIndex]
        this.updateSetting('fontSize', selectedSize.key)
      }
    })
  },

  // 语言设置
  onLanguageTap() {
    const languages = [
      { key: 'zh-CN', name: '简体中文' },
      { key: 'en-US', name: 'English' }
    ]
    
    const itemList = languages.map(item => item.name)
    const currentIndex = languages.findIndex(item => item.key === this.data.settings.language)
    
    wx.showActionSheet({
      itemList,
      success: (res) => {
        const selectedLang = languages[res.tapIndex]
        this.updateSetting('language', selectedLang.key)
      }
    })
  },

  // 清除缓存
  onClearCache() {
    wx.showModal({
      title: '清除缓存',
      content: '确定要清除所有缓存数据吗？',
      success: (res) => {
        if (res.confirm) {
          // 清除缓存
          this.setData({
            'settings.cacheSize': '0MB'
          })
          
          wx.showToast({
            title: '缓存已清除',
            icon: 'success'
          })
        }
      }
    })
  },

  // 检查更新
  onCheckUpdate() {
    wx.showLoading({
      title: '检查更新中...'
    })
    
    // 检查更新
    setTimeout(() => {
      wx.hideLoading()
      wx.showModal({
        title: '检查更新',
        content: '当前已是最新版本',
        showCancel: false
      })
    }, 2000)
  },

  // 关于我们
  onAbout() {
    wx.navigateTo({
      url: '/pages/about/about'
    })
  },

  // 隐私政策
  onPrivacy() {
    wx.showModal({
      title: '隐私政策',
      content: '我们重视您的隐私保护，详细政策请查看官网',
      showCancel: false
    })
  },

  // 用户协议
  onAgreement() {
    wx.showModal({
      title: '用户协议',
      content: '详细协议内容请查看官网',
      showCancel: false
    })
  },

  // 退出登录
  onLogout() {
    if (!this.data.userInfo.isLogin) {
      wx.showToast({
        title: '您还未登录',
        icon: 'none'
      })
      return
    }
    
    wx.showModal({
      title: '退出登录',
      content: '确定要退出登录吗？',
      success: (res) => {
        if (res.confirm) {
          const app = getApp()
          app.clearUserInfo()
          
          this.setData({
            userInfo: { isLogin: false }
          })
          
          wx.showToast({
            title: '已退出登录',
            icon: 'success'
          })
        }
      }
    })
  }
})
